<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Arsip extends CI_Controller
{
	function __construct(){
		parent::__construct();	
		$this->load->library('form_validation');			
		$this->load->model('arsip_model');		
		$this->load->model('organisasi_model');		
		$this->load->model('jenis_model');	
		$this->load->model('klasifikasi_model');
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('session');		
	}
	
	function index()
	{
			if($this->session->userdata('nama') == null){
				redirect('welcome');
			}
			else{
			//ver lib
		
			/*
			 * 0 - display name
			 * 1 - width
			 * 2 - sortable
			 * 3 - align
			 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
			 */
			$colModel['NO'] = array('NO',40,FALSE,'center',0);
			$colModel['UBAH'] = array('EDIT',50,FALSE,'center',0);
			$colModel['LIHAT'] = array('LIHAT',50,FALSE,'center',0);
			$colModel['ARSIP_TAHUN'] = array('TAHUN',120,TRUE,'center',2);
			$colModel['ARSIP_ID'] = array('ARSIP ID',120,TRUE,'center',2);
			$colModel['ARSIP_KODE'] = array('KODE',120,TRUE,'center',2);
			$colModel['ARSIP_ORGANISASI'] = array('ORGANISASI',120,TRUE,'center',2);
			$colModel['ARSIP_JENIS'] = array('JENIS',150,FALSE,'center',2);
			$colModel['ARSIP_KLASIFIKASI'] = array('KLASIFIKASI',120,TRUE,'center',2);
			$colModel['ARSIP_NOMOR'] = array('NOMOR',120,TRUE,'center',2);
			$colModel['ARSIP_TANGGAL'] = array('TANGGAL',120,TRUE,'center',2);
			$colModel['ARSIP_JUDUL'] = array('JUDUL',120,TRUE,'center',2);
			$colModel['ARSIP_KATAKUNCI'] = array('KATAKUNCI',100,TRUE,'center',2);
			$colModel['ARSIP_RINGKASAN'] = array('RINGKASAN',100,TRUE,'center',2);
			$colModel['ARSIP_SIFAT'] = array('SIFAT',100,FALSE,'center',2);
			$colModel['ARSIP_KOTA'] = array('KOTA',100,FALSE,'center',2);
			$colModel['ARSIP_JABATAN'] = array('JABATAN',100,FALSE,'center',2);
			$colModel['ARSIP_PEJABAT'] = array('PEJABAT',120,FALSE,'center',2);
					
			
			/*
			 * Aditional Parameters
			 */
			$gridParams = array(
			'width' => 'auto',
			'height' => '335',
			'rp' => 15,
			'rpOptions' => '[10,15,20,25,40]',
			'pagestat' => 'Displaying: {from} to {to} of {total} items.',
			'blockOpacity' => 0.5,
			'title' => 'Daftar Keseluruhan Arsip'
			);
			
			/*
			 * 0 - display name
			 * 1 - bclass
			 * 2 - onpress
			 */
			$buttons[] = array('Tambah Data','add','arsip_js');
			$buttons[] = array('Hapus Data','delete','arsip_js');
			$buttons[] = array('separator');
			$buttons[] = array('Pilih Semua','add','arsip_js');
			$buttons[] = array('Batal Memilih Semua','delete','arsip_js');
			$buttons[] = array('separator');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/arsip/arsip_grid/"),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"
			<script type='text/javascript'>
						
			function arsip_js(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Batal Memilih Semua')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah Data')
				{
					location.href='".base_url()."index.php/arsip/input_arsip/'; 
				}
				
				if (com=='Hapus Data')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin menghapus ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".site_url('/arsip/delete_arsip')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}          
			}
			</script>
			";
		
		/* $data['notification'] = "";
		if($this->session->userdata('notification')!=''){
			$data['notification'] = "
				<script>
					$(document).ready(function() {
						$.growlUI('Pesan :', '".$this->session->userdata('notification')."'); 
					});
				</script>
			";
		}//end if */	
		
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
		//$this->load->view('grid',$data);
		//$this->load->view('flexigrid',$data);
		}
	}
	
	function arsip_grid()
	{
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('ARSIP_TAHUN','ARSIP_ID','ARSIP_KODE','ARSIP_ORGANISASI','ARSIP_JENIS','ARSIP_KLASIFIKASI','ARSIP_NOMOR','ARSIP_TANGGAL','ARSIP_JUDUL',
			'ARSIP_KATAKUNCI','ARSIP_RINGKASAN','ARSIP_SIFAT','ARSIP_KOTA','ARSIP_JABATAN','ARSIP_PEJABAT');
		
		$this->flexigrid->validate_post('ARSIP_ID','asc',$valid_fields);

		$records = $this->arsip_model->get_arsip_grid();
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 1;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{

			$record_items[] = array($row->ARSIP_TAHUN.$row->ARSIP_ID,
			$no,
			'<a href=\''.site_url().'/arsip/edit_arsip/'.$row->ARSIP_TAHUN.$row->ARSIP_ID.'/\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> ',
			'<a href=\''.site_url().'/scan/scan/'.$row->ARSIP_TAHUN.$row->ARSIP_ID.'/\'><img border=\'0\' src=\''.base_url().'images/main/dir.png\'></a> ',
			$row->ARSIP_TAHUN,
			$row->ARSIP_ID,
			$row->ARSIP_KODE,
			$row->ARSIP_ORGANISASI,
			$row->ARSIP_JENIS,
			$row->ARSIP_KLASIFIKASI,
			$row->ARSIP_NOMOR,
			$row->ARSIP_TANGGAL,
			$row->ARSIP_JUDUL,
			$row->ARSIP_KATAKUNCI,
			$row->ARSIP_RINGKASAN,
			$row->ARSIP_SIFAT,
			$row->ARSIP_KOTA,
			$row->ARSIP_JABATAN,
			$row->ARSIP_PEJABAT
									
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('arsip_id', 'Arsip ID', 'required|is_unique');
			$this->form_validation->set_rules('arsip_tahun', 'Arsip Tahun', 'callback_cek_dropdown');
			$this->form_validation->set_rules('arsip_tanggal', 'Arsip Tanggal', 'required');
			$this->form_validation->set_rules('arsip_organisasi', 'Arsip Organisasi', 'callback_cek_dropdown_org');
			$this->form_validation->set_rules('arsip_jenis', 'Arsip Jenis', 'callback_cek_dropdown_jns');
			$this->form_validation->set_rules('arsip_klasifikasi', 'Arsip Klasifikasi', 'callback_cek_dropdown_kls');
			$this->form_validation->set_rules('arsip_sifat', 'Arsip Sifat', 'is_numeric');
						
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			$this->form_validation->set_message('is_unique', 'Data sudah ada dalam database');
			$this->form_validation->set_message('is_numeric', 'Kolom %s harus diisi dengan angka');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}

	function cek_dropdown($value){
	$thn= $this->input->post('arsip_tahun');
		if($thn =='0'){
			$this->form_validation->set_message('cek_dropdown', 'Kolom %s harus dipilih!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}

	function cek_dropdown_org($value){
	$org= $this->input->post('arsip_organisasi');
		if($org == 'org'){
			$this->form_validation->set_message('cek_dropdown_org', 'Kolom %s harus dipilih!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}

	function cek_dropdown_jns($value){
	$jns= $this->input->post('arsip_jenis');
		if($jns == 'jns'){
			$this->form_validation->set_message('cek_dropdown_jns', 'Kolom %s harus dipilih!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}

	function cek_dropdown_kls($value){
	$kls= $this->input->post('arsip_klasifikasi');
		if($kls == 'kls'){
			$this->form_validation->set_message('cek_dropdown_kls', 'Kolom %s harus dipilih!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
	
	function input_arsip()
    {
 		if ($this->cek_validasi() == FALSE){
 			if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get"))
                                    @date_default_timezone_set(@date_default_timezone_get());

			$options['0'] = "-- Pilih Tahun --";
			for($thn=2000;$thn<=date("Y")+5;$thn++)
				{
					$options[$thn] = $thn;
				}

			$organisasi = $this->organisasi_model->get_all_organisasi();
			$organisasi_options['org'] = '-- Pilih Organisasi --';
				foreach($organisasi as $org){
					$organisasi_options[$org->ORG_NAMA] = $org->ORG_NAMA;
				}

			$jenis = $this->jenis_model->get_all_jenis();
			$jenis_options['jns'] = '-- Pilih Jenis --';
				foreach($jenis as $jns){
					$jenis_options[$jns->JENIS_NAMA] = $jns->JENIS_NAMA;
				}

			$klasifikasi = $this->klasifikasi_model->get_all_klasifikasi();
			$klasifikasi_options['kls'] = '-- Pilih Klasifikasi --';
				foreach($klasifikasi as $kls){
					$klasifikasi_options[$kls->KLASIFIKASI_NAMA] = $kls->KLASIFIKASI_NAMA;
				}

			$data['status'] = '';
			$data['options'] = $options;
			$data['organisasi_options'] = $organisasi_options;
			$data['jenis_options'] = $jenis_options;
			$data['klasifikasi_options'] = $klasifikasi_options;
 			$data['arsip_id'] = ''; 			
			$data['content'] = $this->load->view('form_arsip',$data,TRUE);
			$this->load->view('main',$data);
		}
		else{
			$this->insert_arsip();
		}            
    }
	
	function insert_arsip(){
		$tgl1 = str_replace("/","-",$_REQUEST['arsip_tanggal']);
		$f_tgl_arsip = date("d-M-Y", strtotime($tgl1));

		$data = array(
			'ARSIP_TAHUN'	 	=> $this->input->post('arsip_tahun'),
			'ARSIP_ID'	 		=> $this->input->post('arsip_id'),
			'ARSIP_KODE'		=> $this->input->post('arsip_kode'),
			'ARSIP_ORGANISASI'	=> $this->input->post('arsip_organisasi'),
			'ARSIP_JENIS'		=> $this->input->post('arsip_jenis'),
	        'ARSIP_KLASIFIKASI' => $this->input->post('arsip_klasifikasi'),
	        'ARSIP_NOMOR'       => $this->input->post('arsip_nomor'),
	        'ARSIP_TANGGAL'     => $f_tgl_arsip,
	        'ARSIP_JUDUL'       => $this->input->post('arsip_judul'),
	        'ARSIP_KATAKUNCI'   => $this->input->post('arsip_katakunci'),
	        'ARSIP_RINGKASAN'   => $this->input->post('arsip_ringkasan'),
	        'ARSIP_SIFAT'       => $this->input->post('arsip_sifat'),
	        'ARSIP_KOTA'        => $this->input->post('arsip_kota'),
	        'ARSIP_JABATAN'     => $this->input->post('arsip_jabatan'),
	        'ARSIP_PEJABAT' 	=> $this->input->post('arsip_pejabat'),
	        'ARSIP_STATUS'		=> 1
		);
		
		$this->arsip_model->insert_arsip($data);
		redirect('arsip'); 
	}
	
	function edit_arsip($id_arsip)
    {
    	if ($this->cek_validasi() == FALSE)
    	{
    		$arsip_tahun = substr($id_arsip, 0,4);
    		$arsip_id = substr($id_arsip, 4);
    		$arsip = $this->arsip_model->get_arsipID($arsip_tahun,$arsip_id);
 			
 			if(function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get"))
                                    @date_default_timezone_set(@date_default_timezone_get());

			$options['0'] = "-- Pilih Tahun --";
			for($thn=2000;$thn<=date("Y")+5;$thn++)
				{
					$options[$thn] = $thn;
				}

			$organisasi = $this->organisasi_model->get_all_organisasi();
			$organisasi_options['org'] = '-- Pilih Organisasi --';
				foreach($organisasi as $org){
					$organisasi_options[$org->ORG_NAMA] = $org->ORG_NAMA;
				}

			$jenis = $this->jenis_model->get_all_jenis();
			$jenis_options['jns'] = '-- Pilih Jenis --';
				foreach($jenis as $jns){
					$jenis_options[$jns->JENIS_NAMA] = $jns->JENIS_NAMA;
				}

			$klasifikasi = $this->klasifikasi_model->get_all_klasifikasi();
			$klasifikasi_options['kls'] = '-- Pilih Klasifikasi --';
				foreach($klasifikasi as $kls){
					$klasifikasi_options[$kls->KLASIFIKASI_NAMA] = $kls->KLASIFIKASI_NAMA;
				}

			$data['arsip'] = $arsip;
			$data['options'] = $options;
			$data['organisasi_options'] = $organisasi_options;
			$data['jenis_options'] = $jenis_options;
			$data['klasifikasi_options'] = $klasifikasi_options;
 			$data['status'] = 'edit'; 			
			$data['content'] = $this->load->view('form_arsip',$data,TRUE);
			$this->load->view('main',$data);
		}
		else
		{
			$this->update_arsip($id_arsip);
		}
    }
	
	function update_arsip($id_arsip){		
   		$arsip_tahun = substr($id_arsip, 0,4);
   		$arsip_id = substr($id_arsip, 4);
		$tgl1 = str_replace("/","-",$_REQUEST['arsip_tanggal']);
		$f_tgl_arsip = date("d-M-Y", strtotime($tgl1));

			$data = array(
				'ARSIP_TAHUN'	 	=> $this->input->post('arsip_tahun'),
				'ARSIP_ID'	 		=> $this->input->post('arsip_id'),
				'ARSIP_KODE'		=> $this->input->post('arsip_kode'),
				'ARSIP_ORGANISASI'	=> $this->input->post('arsip_organisasi'),
				'ARSIP_JENIS'		=> $this->input->post('arsip_jenis'),
		        'ARSIP_KLASIFIKASI' => $this->input->post('arsip_klasifikasi'),
		        'ARSIP_NOMOR'       => $this->input->post('arsip_nomor'),
		        'ARSIP_TANGGAL'     => $f_tgl_arsip,
		        'ARSIP_JUDUL'       => $this->input->post('arsip_judul'),
		        'ARSIP_KATAKUNCI'   => $this->input->post('arsip_katakunci'),
		        'ARSIP_RINGKASAN'   => $this->input->post('arsip_ringkasan'),
		        'ARSIP_SIFAT'       => $this->input->post('arsip_sifat'),
		        'ARSIP_KOTA'        => $this->input->post('arsip_kota'),
		        'ARSIP_JABATAN'     => $this->input->post('arsip_jabatan'),
		        'ARSIP_PEJABAT' 	=> $this->input->post('arsip_pejabat')
			);
			
		$this->arsip_model->update_arsip($data,$arsip_tahun,$arsip_id);
		redirect('arsip');		 
	}
	
	//Delete
	function delete_arsip()
	{
		$id_arsips_post_array = explode(",",$this->input->post('items'));
		$msg = '';
		foreach($id_arsips_post_array as $index => $id_arsip){
			if (isset($id_arsip) && $id_arsip != "") {	
	   			$arsip_tahun = substr($id_arsip, 0,4);
	   			$arsip_id = substr($id_arsip, 4);
				$this->arsip_model->delete_arsip($arsip_tahun,$arsip_id);
						
			
			$msg .= "Arsip dengan tahun ".$arsip_tahun." dan nomor (".$arsip_id.") telah berhasil dihapus.\n";
			}
		}
		$this->output->set_header($this->config->item('ajax_header'));
		$this->output->set_output($msg);
	}

}//end of class